package com.hb.trade.server.project.opening.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.project.opening.dto.BidOpeningScheduleDTO;
import com.hb.core.pojo.project.opening.dto.BidOpeningScheduleTemplateDTO;
import com.hb.core.pojo.project.opening.entity.BidOpeningSchedule;
import com.hb.core.pojo.project.opening.entity.BidOpeningScheduleTemplate;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.core.pojo.project.section.entity.BidSection;
import com.hb.trade.server.project.opening.manager.BidOpeningScheduleManager;
import com.hb.trade.server.project.opening.service.BidOpeningScheduleService;
import com.hb.trade.server.project.opening.service.BidOpeningScheduleTemplateService;
import com.hb.trade.server.project.section.service.raw.BidSectionService;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 开标一览表 前端控制器
 *
 * @author zhongdl
 * @since 2021-09-17
 */
@RestController
public class BidOpeningScheduleController {

    protected final BidOpeningScheduleService bidOpeningScheduleService;
    protected final BidOpeningScheduleTemplateService bidOpeningScheduleTemplateService;
    protected final BidSectionService bidSectionService;

    public BidOpeningScheduleController(BidOpeningScheduleService bidOpeningScheduleService,
                                        BidOpeningScheduleTemplateService bidOpeningScheduleTemplateService,
                                        BidSectionService bidSectionService) {
        this.bidOpeningScheduleService = bidOpeningScheduleService;
        this.bidOpeningScheduleTemplateService = bidOpeningScheduleTemplateService;
        this.bidSectionService = bidSectionService;
    }

    /**
     * 保存该招标文件的开标一览表
     *
     * @return
     */
    @PostMapping("/bidOpeningSchedule/saveSchedulesToBidFile")
    public Message<Boolean> saveSchedulesToBidFile(@RequestBody BidOpeningScheduleDTO bidOpeningInfo) {
        Long fileId = bidOpeningInfo.getFileId();
        if (fileId == null) {
            return MsgUtil.fail("招标文件id不能为空");
        }
        List<BidOpeningSchedule> bidOpeningSchedules = bidOpeningInfo.getBidOpeningSchedules();
        if (CollUtil.isEmpty(bidOpeningSchedules)) {
            return MsgUtil.fail("至少添加一项开标项");
        }

        //删除之前招标文件关联开标一览表的信息
        QueryWrapper<BidOpeningSchedule> bidOpeningInfoDelete = new QueryWrapper<>();
        bidOpeningInfoDelete.lambda().eq(BidOpeningSchedule::getFileId, fileId);

        bidOpeningScheduleService.remove(bidOpeningInfoDelete);

        //新增
        BidOpeningScheduleManager scheduleManager = bidOpeningInfo.to(BidOpeningScheduleManager.class);
        List<BidOpeningSchedule> batchData = scheduleManager.getBatchData();
        boolean save = bidOpeningScheduleService.saveBatch(batchData);

        return MsgUtil.success(save);
    }

    /**
     * 查询常用开标项
     * 先查询字典信息中的常用开标项
     * 再追加自定义的常用开标项
     *
     * @return
     */
    @PostMapping("/bidOpeningSchedule/listByFileId")
    public Message listByFileId(@RequestParam("fileId") Long fileId, @RequestParam("projectType") Integer projectType) {
        if (fileId == null) {
            return MsgUtil.success(CollUtil.list(false));
        }
        //查询所有常用的开标项
        List<BidOpeningSchedule> bidOpeningInfoList = bidOpeningScheduleService.listByFileId(fileId);

        // 开标一览表始终有值
        if (CollUtil.isEmpty(bidOpeningInfoList)) {
            // 查询默认的开标项
            BidOpeningScheduleTemplateDTO bidOpeningScheduleTemplateDTO = new BidOpeningScheduleTemplateDTO();
            bidOpeningScheduleTemplateDTO.setOnlyDefault(true);
            bidOpeningScheduleTemplateDTO.setProjectType(projectType);
            Message<List<BidOpeningScheduleTemplate>> listMessage = scheduleTemplates(bidOpeningScheduleTemplateDTO);
            for (BidOpeningScheduleTemplate bidOpeningScheduleTemplate : listMessage.getData()) {
                BidOpeningSchedule to = bidOpeningScheduleTemplate.to(BidOpeningSchedule.class);
                bidOpeningInfoList.add(to);
            }
        }

        return MsgUtil.success(bidOpeningInfoList);
    }

    /**
     * 查询开标一览表的通用模板
     *
     * @param bidOpeningScheduleTemplateDTO
     * @return
     */
    @PostMapping("/bidOpeningSchedule/scheduleTemplates")
    public Message<List<BidOpeningScheduleTemplate>> scheduleTemplates(@RequestBody BidOpeningScheduleTemplateDTO bidOpeningScheduleTemplateDTO) {
        QueryWrapper<BidOpeningScheduleTemplate> wrapper =
                BidOpeningScheduleTemplate.wrapper("id", "bid_opening_name", "unit_name", "fill_type", "sort_num");
        // 按照项目分类查询 常用开标项
        List<Integer> projectTypes = new ArrayList<>();
        projectTypes.add(4); // 通用项
        projectTypes.add(bidOpeningScheduleTemplateDTO.getProjectType()); // 当前项目分类
        wrapper.in("project_type", projectTypes);

        wrapper.orderByAsc("sort_num");
        if (null != bidOpeningScheduleTemplateDTO.getOnlyDefault() && bidOpeningScheduleTemplateDTO.getOnlyDefault()) {
            wrapper.eq("default_flag", 1);
        }
        List<BidOpeningScheduleTemplate> templates = bidOpeningScheduleTemplateService.list(wrapper);
        return MsgUtil.success(templates);
    }

    /**
     * 根据标段Id查询开标一览表模板
     *
     * @param bidSectionDTO
     * @return
     */
    @PostMapping("/bidOpeningSchedule/listBySectionId")
    public Message<List<BidOpeningSchedule>> listBySectionId(@RequestBody BidSectionDTO bidSectionDTO) {
        if (null == bidSectionDTO.getSectionId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        //获取标段信息
        BidSection bidSection = bidSectionService.getById(bidSectionDTO.getSectionId());
        if (null == bidSection) {
            return MsgUtil.fail("未获取到相关标段信息");
        }
        Long fileId = bidSection.getFileId();
        if (null == fileId) {
            return MsgUtil.fail("未获取到招标文件信息");
        }
        //查询所有常用的开标项
        List<BidOpeningSchedule> bidOpeningInfoList = bidOpeningScheduleService.listByFileId(fileId);
        return MsgUtil.success(bidOpeningInfoList);
    }
}
